Editing an Archetype in Code

Often times you may want to edit an Archetype or create a new one altogether. The following example illustrates how to retrieve an Archetype from ContentService, edit it and save it back.

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Archetype.Models;
  4. using Newtonsoft.Json;
  5. using Umbraco.Core;
  6. namespace MyApp
  7. {
  8. public class MyClass
  9. {
  10. public void MyMethod()
  11. {
  12. //get a ref to services
  13. var services = ApplicationContext.Current.Services;
  14. //get a reference to the current content if an edit (or create a new node instead)
  15. var content = services.ContentService.GetById(1234);
  16. var currentArchetypeJson = content.GetValue<string>("myDoctypeAliasForArchetypeProperty");
  17. //deserialize into an Archetype
  18. var myArchetype = JsonConvert.DeserializeObject<ArchetypeModel>(currentArchetypeJson);
  19. //get the current fieldsets
  20. var currentFieldsets = new List<ArchetypeFieldsetModel>();
  21. currentFieldsets = myArchetype.Fieldsets.ToList();
  22. //edit some of the fieldsets or properties if you want
  23. var someCurrentFieldsets = currentFieldsets.Where(x => x.Alias == "someFieldsetAlias");
  24. foreach (var fieldset in someCurrentFieldsets)
  25. {
  26. var property = fieldset.Properties.FirstOrDefault(x => x.Alias == "somePropertyAlias");
  27. if (property != null)
  28. {
  29. property.Value = "new value";
  30. }
  31. }
  32. //add some new fieldsets
  33. currentFieldsets.AddRange(
  34. new List<ArchetypeFieldsetModel>(){
  35. //fieldset 1 of type 'myFieldsetAlias'
  36. new ArchetypeFieldsetModel() {
  37. Alias = "myFieldsetAlias",
  38. Properties = new List<ArchetypePropertyModel>()
  39. {
  40. //property 1 of type 'myFieldsetAlias'
  41. new ArchetypePropertyModel() {
  42. Alias = "myPropertyAlias",
  43. Value = ""
  44. },
  45. new ArchetypePropertyModel() {
  46. Alias = "myPropertyAlias2",
  47. Value = ""
  48. },
  49. new ArchetypePropertyModel() {
  50. Alias = "myPropertyAlias3",
  51. Value = ""
  52. }
  53. },
  54. Disabled = false,
  55. },
  56. //fieldset 2 of type 'myFieldsetAlias'
  57. new ArchetypeFieldsetModel() {
  58. Alias = "myFieldsetAlias",
  59. Properties = new List<ArchetypePropertyModel>()
  60. {
  61. new ArchetypePropertyModel() {
  62. Alias = "myPropertyAlias",
  63. Value = ""
  64. },
  65. new ArchetypePropertyModel() {
  66. Alias = "myPropertyAlias2",
  67. Value = ""
  68. },
  69. new ArchetypePropertyModel() {
  70. Alias = "myPropertyAlias3",
  71. Value = ""
  72. }
  73. },
  74. Disabled = false,
  75. }
  76. //keep adding as you like
  77. });
  78. //add our edited fieldsets to the Archetype
  79. myArchetype.Fieldsets = currentFieldsets;
  80. //generate the json that needs to be saved
  81. var json = myArchetype.SerializeForPersistence();
  82. //use ContentService or MemberService etc to save it to a node
  83. content.SetValue("myDoctypeAliasForArchetypeProperty", json);
  84. services.ContentService.Save(content);
  85. }
  86. }
  87. }

Note if you want to create an Archetype instead of loading it, just instantiate a new ArchetypeModel instead of loading and deserializing it.